package de.osflex.touarek.containers
{
	   
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Panel;
	import mx.core.SpriteAsset;
	import mx.core.UIComponent;
	import mx.core.WindowedApplication;
	import mx.events.FlexEvent;	 
	
	/**
	 * @author amoerke
	 * @created 20080914
	 * @class DragPanel
	 * 
	 * Class extends the Standard Panel class
	 * with Minimize and Maximize buttons
	 */

    public class DragPanel extends Panel
    {
    	protected var _bolIsMainPanel:Boolean = false;
    	protected var minShape:SpriteAsset;
     	protected var restoreShape:SpriteAsset;
     	protected var exitShape:SpriteAsset;
     	
     	private var myTitleBar:UIComponent;
     	private var xOff:Number;
        private var yOff:Number;
        
        /**
        * Setter sets the Panel as Mainpanel means it contains a
        * ApplicationClose button
        * @param bolValue Boolean
        */ 

		public function set isMainPanel(bolValue:Boolean):void
		{
			this._bolIsMainPanel = bolValue;
		}
		
		/**
		 * Getter-Method
		 */
		
		public function get isMainPanel():Boolean
		{
			return this._bolIsMainPanel;
		}		
    	
    	/**
    	 * Add the creationComplete event handler.
    	 */ 
         
        public function DragPanel()
        {
            super();
            addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
        }
        
        /**
        * Grab the titleBar to work with it
        */
                    
        private function creationCompleteHandler(event:Event):void
        {
            myTitleBar = titleBar;            
            // Add the resizing event handler.    
            addEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
            myTitleBar.addEventListener(MouseEvent.MOUSE_DOWN, tbMouseDownHandler);
            myTitleBar.addEventListener(MouseEvent.MOUSE_UP, tbMouseUpHandler);      
        }
        
        /**
        * MouseDownHandler to move the Panel
        * @param event MouseEvent
        */
                    
        private function tbMouseDownHandler(event:MouseEvent):void {
            
            xOff = event.currentTarget.mouseX;
            yOff = event.currentTarget.mouseY;
            parent.addEventListener(MouseEvent.MOUSE_MOVE, tbMouseMoveHandler);
            parent.setChildIndex(this,parent.numChildren-1);            
        }
        
       /**
       * Move the Panel
       * 
       * @param event MouseEvent
       */ 
            
       private function tbMouseMoveHandler(event:MouseEvent):void {
            
            // Compensate for the mouse pointer's location in the title bar.
            
            var tempX:int = parent.mouseX - xOff;
            x = tempX;
            
            var tempY:int = parent.mouseY - yOff;
            y = tempY;    
                    
        }
        
        /**
        * Set the MouseUpHandler
        * 
        * @param event MouseEvent
        */
        
        private function tbMouseUpHandler(event:MouseEvent):void {

           parent.removeEventListener(MouseEvent.MOUSE_MOVE, tbMouseMoveHandler);      
        }  
        
        /**
        * Extend the offical createChildren method to add the controls
        */

        override protected function createChildren():void
        {
                super.createChildren();
            
            // Create the SpriteAsset's for the min/restore icons and 
            // add the event handlers for them.
            minShape = new SpriteAsset();
            minShape.addEventListener(MouseEvent.MOUSE_DOWN, minPanelSizeHandler);
            titleBar.addChild(minShape);

            restoreShape = new SpriteAsset();
            restoreShape.addEventListener(MouseEvent.MOUSE_DOWN, restorePanelSizeHandler);
            titleBar.addChild(restoreShape);
            
            if(this._bolIsMainPanel){
            	exitShape = new SpriteAsset();
            	exitShape.addEventListener(MouseEvent.MOUSE_DOWN, exitApplication);
            	titleBar.addChild(exitShape);
            }          
        }
        
        /**
        * exitHandler
        * 
        * @param event MouseEvent
        */
        
        protected function exitApplication(event:MouseEvent):void
        {
        	WindowedApplication(this.parentApplication).close();
        }
        
        /**
        * Extends the official updateDisplayList method
        * for repositioning of the contols
        * 
        * @param unscaledWidth Number
        * @param unscaledHeight Number
        * 
        */
            
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            
            var subForMain:uint = this._bolIsMainPanel ? 15 : 0; 
            
            // Create invisible rectangle to increase the hit area of the min icon.
            minShape.graphics.clear();
            minShape.graphics.lineStyle(0, 0, 0);
            minShape.graphics.beginFill(0xFFFFFF, 0.0);
            minShape.graphics.drawRect(unscaledWidth - (35 + subForMain), 12, 8, 8);

            // Draw min icon.
            minShape.graphics.lineStyle(1, 0xAAAAAA);
            minShape.graphics.beginFill(0xFFFFFF, 0.0);
            minShape.graphics.drawRect(unscaledWidth - (35 + subForMain), 18, 8, 2);
                
            // Draw restore icon.
            restoreShape.graphics.clear();
            restoreShape.graphics.lineStyle(1, 0xAAAAAA);
            restoreShape.graphics.beginFill(0xFFFFFF, 0.0);
            restoreShape.graphics.drawRect(unscaledWidth - (20 + subForMain), 12, 8, 8);
            restoreShape.graphics.moveTo(unscaledWidth - (20 + subForMain), 15);
            restoreShape.graphics.lineTo(unscaledWidth - (12 + subForMain), 15);
            
            // Draw exit icon
            if(this._bolIsMainPanel){
	            exitShape.graphics.clear();
	            exitShape.graphics.lineStyle(1, 0xAAAAAA);
	            exitShape.graphics.beginFill(0xFFFFFF, 0.0);
	            exitShape.graphics.drawRect(unscaledWidth - 20, 12, 8,8);  
	            exitShape.graphics.moveTo(unscaledWidth - 20, 12);
	            exitShape.graphics.lineTo(unscaledWidth - 12, 20);       
	            exitShape.graphics.moveTo(unscaledWidth - 11, 12);
	            exitShape.graphics.lineTo(unscaledWidth - 20, 20);
            }
            
            // Draw resize graphics if not minimzed.                
            graphics.clear()
            if (isMinimized == false)
            {
                graphics.lineStyle(2, 0xAAAAAA);
                graphics.moveTo(unscaledWidth - 6, unscaledHeight - 1)
                graphics.curveTo(unscaledWidth - 3, unscaledHeight - 3, unscaledWidth - 1, unscaledHeight - 6);                        
                graphics.moveTo(unscaledWidth - 6, unscaledHeight - 4)
                graphics.curveTo(unscaledWidth - 5, unscaledHeight - 5, unscaledWidth - 4, unscaledHeight - 6);                        
            }
        }
                    
        private var myRestoreHeight:int;
        private var isMinimized:Boolean = false; 
        
        /**
        *  Minimize panel event handler.
        *  @param event Event
        */            
        
        private function minPanelSizeHandler(event:Event):void
        {
            if (isMinimized != true)
            {
                myRestoreHeight = height;    
                height = titleBar.height;
                isMinimized = true;    
                // Don't allow resizing when in the minimized state.
                removeEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
            }                
        }
        
        /**
        * Restore panel event handler.
        * 
        * @param event Event
        */
        
        private function restorePanelSizeHandler(event:Event):void
        {
            if (isMinimized == true)
            {
                height = myRestoreHeight;
                isMinimized = false;    
                // Allow resizing in restored state.                
                addEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
            }
        }

        // Define static constant for event type.
        //public static const RESIZE_CLICK:String = "resizeClick";
        
        private var origWidth:int;
        private var origHeight:int;
        
        
        /**
        * Resize panel event handler.
        * 
        * @param event MouseEvent
        */
        
        public  function resizeHandler(event:MouseEvent):void
        {
            // Determine if the mouse pointer is in the lower right 7x7 pixel
            // area of the panel. Initiate the resize if so.
            
            // Lower left corner of panel
            var lowerLeftX:Number = x + width; 
            var lowerLeftY:Number = y + height;
                
            // Upper left corner of 7x7 hit area
            var upperLeftX:Number = lowerLeftX-7;
            var upperLeftY:Number = lowerLeftY-7;
                
            // Mouse positionin Canvas
            var panelRelX:Number = event.localX + x;
            var panelRelY:Number = event.localY + y;

            // See if the mousedown is in the lower right 7x7 pixel area
            // of the panel.
            if (upperLeftX <= panelRelX && panelRelX <= lowerLeftX)
            {
                if (upperLeftY <= panelRelY && panelRelY <= lowerLeftY)
                {    
                
                    
                    event.stopPropagation();        
                    
                    origWidth = width;
                    origHeight = height;
                    xOff = parent.mouseX;
                    yOff = parent.mouseY;
                    parent.addEventListener(MouseEvent.MOUSE_MOVE, resizePanel);
                    parent.addEventListener(MouseEvent.MOUSE_UP, stopResizePanel);
                    
                }
            }                
        }
        
        /**
        * Resize the panel at the end
        * 
        * @param event MouseEvent
        */
        
        private function resizePanel(event:MouseEvent):void {
            
            if ((origWidth + (parent.mouseX - xOff)) > 250){
                width = origWidth + (parent.mouseX - xOff);    
            }
            
            if ((origHeight + (parent.mouseY - yOff)) > titleBar.height){
                height = origHeight + (parent.mouseY - yOff);
            }
                    
        }
        
        /**
        * Stop resizing the panel
        * 
        * @param event MouseEvent
        */
        
        private function stopResizePanel(event:MouseEvent):void {
            parent.removeEventListener(MouseEvent.MOUSE_MOVE, resizePanel);
        }
        
                
    }

}